package com.chhuang.huaao.thread;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.chhuang.utils.Assert;
import com.chhuang.utils.file.FileUtil;

/**
 * 把token写入文件
 * @author CHHUANG
 *
 */
public class TokenFileThread implements Runnable {
	
	private Map<String, JSONObject> tokens;
	private String filePath;
	
	public TokenFileThread(Map<String, JSONObject> tokens, String filePath){
		this.tokens = tokens;
		this.filePath = filePath;
	}

	@Override
	public void run() {
		if(tokens==null || tokens.isEmpty() || Assert.isInvalidString(filePath)){
			return ;
		}
		
		synchronized(this){
			File file = new File(filePath);
			FileUtil.makeFilePath(file);
			try {
				FileOutputStream fout = new FileOutputStream(file);
				ObjectOutputStream output = new ObjectOutputStream(fout);
				output.writeObject(tokens);
				output.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
